﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils.Controls;
using DevExpress.XtraEditors.DXErrorProvider;
using SVC.client;
namespace UI.Forms
{
    public partial class Login : DevExpress.XtraEditors.XtraForm
    {
        BackgroundWorker bgwork = null;
        bool linked = false;
        public Login()
        {
            InitializeComponent();
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(MyConfig.getAppValue("SkinName"));
            ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule();
            notEmptyValidationRule.ConditionOperator = ConditionOperator.IsNotBlank;
            notEmptyValidationRule.ErrorText = "请选择一个用户！";
            //ConditionValidationRule notEmptyValidationRule1 = new ConditionValidationRule();
            //notEmptyValidationRule1.ConditionOperator = ConditionOperator.IsNotBlank;
            //notEmptyValidationRule1.ErrorText = "请输入密码！";

            dxValidationProvider1.SetValidationRule(lueuser, notEmptyValidationRule);
            //dxValidationProvider1.SetValidationRule(txtpass, notEmptyValidationRule1);
            CheckForIllegalCrossThreadCalls = false;
            bgwork = new BackgroundWorker();

            bgwork.DoWork += new DoWorkEventHandler(bgwork_DoWork);
            bgwork.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwork_RunWorkerCompleted);
        }

        void bgwork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (linked)
            {
                my.ACID = 1;
                lueuser.Properties.DataSource = my.bw.View_Emp_lite_List("",1);

                pictureBox3.Image = imageList1.Images[1];
                btn_login.Enabled = true;
                btnlink.Visible = false;
                btn_set.Enabled = false;
                lbllinkinfo.ForeColor = Color.Green;
                lbllinkinfo.Text = "连接服务器成功！";
                lueuser.KeyUp += new KeyEventHandler(lueuser_KeyUp);
                
                Guid uid = Guid.Empty;
                try
                {
                    uid = new Guid(MyConfig.getAppValue("lastlogonuser"));
                }
                catch
                {


                }
                if (uid.Equals(Guid.Empty))
                {
                    lueuser.ShowPopup();

                }
                else
                {
                    lueuser.EditValue = uid;
                    txtpass.Focus();
                }

                txtpass.KeyUp += new KeyEventHandler(txtpass_KeyUp);
            }
            else
            {
                pictureBox3.Image = imageList1.Images[2];
                btn_login.Enabled = false;
                btnlink.Visible = true;
                btn_set.Enabled = true;
                lbllinkinfo.ForeColor = Color.Red;
                lbllinkinfo.Text = "连接服务器失败！";

            }
            btnlink.Enabled = true;
            btn_set.Enabled = true;
            myOpaqueLayer1.SendToBack();
            myOpaqueLayer1.Visible = false;
        }

        void bgwork_DoWork(object sender, DoWorkEventArgs e)
        {
            btnlink.Enabled = false;
            myOpaqueLayer1.Visible = true;
            myOpaqueLayer1.BringToFront();

            linked = my.pwpid == "pid";



        }

        private void Login_Load(object sender, EventArgs e)
        {
            
            pictureBox3.Image = imageList1.Images[2];
            check();
        }

        void lueuser_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue==13)
            {
                txtpass.Focus();
            }
        }

        void txtpass_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue==13)
            {
                btn_login_Click(null, null);
            }
        }


        private void btnlink_Click(object sender, EventArgs e)
        {

            check();
        }

        private void check()
        {
            lbllinkinfo.ForeColor = Color.Black;
            lbllinkinfo.Text = "正在连接服务器";
            if (!bgwork.IsBusy)
            {
                bgwork.RunWorkerAsync();
            }
            
            
        }



        private void btn_login_Click(object sender, EventArgs e)
        {
            btn_login.Enabled = false;
            lbllinkinfo.ForeColor = Color.Black;
            lbllinkinfo.Text = "正在登陆";
            if(!dxValidationProvider1.Validate())
            {
                lbllinkinfo.Text = "";
                btn_login.Enabled = true;
                return;
              
            }

            my.chekPrint();
            if (my.bw.vaildUser(lueuser.EditValue.ToString(), MyEncrypt.MD5Encrypt(txtpass.EditValue.ToString().Trim()),my.ACID))
            {

                lbllinkinfo.ForeColor = Color.Green;
                lbllinkinfo.Text = "登录成功";
                my.appName = "电话卡销售系统";
                my.corName = "卡通";
                my.emp_lite = my.bw.View_Emp_Lite_by_EmpID(lueuser.EditValue.ToString(),my.ACID);
                my.logintime = DateTime.Now;
               //my.mypowertree = my.bw.getPowerTreeByuid((Guid)lueuser.EditValue);
               //Sys_Log logger = new Sys_Log();
               //logger.UID = my.mypowertree.User.UID;
               //logger.LogType = 1;
               //logger.Cdate = DateTime.Now;
               //logger.Message = string.Format("{0}成功登录", my.mypowertree.User.UserNo);
               //my.bw.AddSys_Log(logger);
                if (my.mainform==null)
                {
                    my.mainform = new MainForm();
                    
                }
                //MyConfig.setAppValue("lastlogonuser", my.mypowertree.User.UID.ToString());
                my.mainform.Show();
                infofloat frm = new infofloat();
                frm.Show();
                this.Hide();
            }
            else
            {
                lbllinkinfo.ForeColor = Color.Red;
                lbllinkinfo.Text = "登录失败!";
                txtpass.Focus();
                txtpass.SelectAll();
            }
            btn_login.Enabled = true; 
            
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void btn_set_Click(object sender, EventArgs e)
        {
            netSetWindow frm = new netSetWindow();
            frm.FatherRefresh = check;
            frm.ShowDialog();
            
        }
    }
}